package org.jeecg.modules.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo {

    public void test() {
        ExecutorService exe = Executors.newFixedThreadPool(10);
        List<String> list = new ArrayList<>();
        int size = 20;
        List<String> list1 = Collections.synchronizedList(new ArrayList());
        for (int i = 0; i < 100; i++) {
            list.add(i+"");
        }
        for (int i = 0; i < 100; i++) {
            int endIndex = 0;
            if((i+1)*size>=list.size())
            {
                endIndex = list.size();
            }
            exe.execute( (Runnable) new TestThread(list,list1,i*size,(i+1)*size));
            if(endIndex == list.size())
            {
                break;
            }
        }
        exe.shutdown();
        while (true) {
            if (exe.isTerminated()) {
                System.out.println("线程结束");
                System.out.println(list1.size());
                System.out.println(list1.toString());
                break;
            }
        }
    }

    class TestThread extends Thread {
        List<String> list;
        List<String> list1;
        int startIndex;
        int endIndex;

        TestThread(List<String> list,List<String> list1,int startIndex,int endIndex) {
            this.list = list;
            this.list1 = list1;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }

        public void run(){
            for(int i=startIndex;i<endIndex;i++)
            {
                list1.add(list.get(i));
            }
        }
    }
}
